﻿@page "/"
@inherits BasePage
@attribute [ReuseTabsPage(Title = Language.Home, Pin = true, Closable = false)]

@{
    // 系统PC端首页组件，可以在此定制首页
}

<AntRow><AntCol Span="24"><KnownCard /></AntCol></AntRow>
<AntRow>
    <AntCol Span="24">
        <SpaceCard @ref="space" />
    </AntCol>
</AntRow>
<AntRow Gutter="10">
    <AntCol Span="14">
        <ChartCard @ref="chart" Title="@Language.HomeLogStatistic" />
    </AntCol>
    <AntCol Span="10">
        <CommFuncCard @ref="func" />
    </AntCol>
</AntRow>

@code {
    private IHomeService Service;
    private SpaceCard space;
    private ChartCard chart;
    private CommFuncCard func;

    /// <inheritdoc />
    public override RenderFragment GetPageTitle()
    {
        return GetPageTitle("home", Language.Home);
    }

    /// <inheritdoc />
    protected override async Task OnInitPageAsync()
    {
        await base.OnInitPageAsync();
        Service = await CreateServiceAsync<IHomeService>();
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            var info = await Service.GetHomeAsync();
            var counts = new List<StatisticCountInfo>();
            counts.Add(new StatisticCountInfo { Name = Language.HomeUserCount, Count = info?.Statistics?.UserCount });
            counts.Add(new StatisticCountInfo { Name = Language.HomeLogCount, Count = info?.Statistics?.LogCount });
            space?.SetCounts(counts);

            var option = new ChartCardOption { Id = "Order", Title = Language.HomeLogStatistic };
            option.Charts.Add(new CardChartInfo
            {
                Type = "Bar",
                Title = Language[Language.HomeVisitTitle].Replace("{month}", $"{DateTime.Now:yyyyMM}"),
                Datas = info?.Statistics?.LogDatas
            });
            await chart?.SetOptionAsync(option);
            func?.SetMenus(info?.VisitMenuIds);
        }
    }
}